package com.uniview.mvpdemo.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.uniview.mvpdemo.R;
import com.uniview.mvpdemo.adapter.Fragment2Adapter;
import com.uniview.mvpdemo.mvp.Fragment2Contract;
import com.uniview.mvpdemo.mvp.Fragment2Presenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Lenovo on 2017/4/24.
 */

public class Main2Fragment extends Fragment implements Fragment2Contract.View {

    private static final String key = "status";
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    Unbinder unbinder;

    private String status;

    private Fragment2Contract.Presenter presenter;
    private LinearLayoutManager linearLayoutManager;
    private Fragment2Adapter adapter;

    public static Main2Fragment newInstance(String status) {
        Main2Fragment fragment = new Main2Fragment();

        Bundle bundle = new Bundle();
        bundle.putString(key, status);
        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        status = getArguments().getString(key);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.fragment_main2, container, false);

        unbinder = ButterKnife.bind(this, v);

        new Fragment2Presenter(this);

        return v;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void setPresenter(Fragment2Contract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void initViews() {
        linearLayoutManager = new LinearLayoutManager(getContext());

        adapter = new Fragment2Adapter(presenter.getData(getContext(), status));

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onLoading(String text) {

    }

    @Override
    public void loadingComplete() {

    }

    @Override
    public void loadingError(String error) {

    }
}
